<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<script type="text/javascript">
    var groupId;
		$().ready(function(){
			$('#newGroupBtn').die('click').live('click',function(){
				var params={
						  'title':'新增组织机构',
						  'url':$(this).attr('url')+"?groupId="+groupId,
						  'width':500,
						  'height':350
						 };
				createIframeDialog(params);
			});
			$('#editGroupBtn').die('click').live('click',function(){
				var params={
						  'title':'编辑组织机构',
						  'url':$(this).attr('url')+"&id="+groupId,
						  'width':500,
						  'height':350,
						  btnSucFunc:function(){
							  window.location.reload();
						  }
						 };
				createIframeDialog(params);
			});
			$('#delGroupBtn').die('click').live('click',function(){
				var groupName=$("#"+groupId).find("a:not(ins)").text();
				if($("#noParentTr").length>0){
					createAlertDialog('提示信息','最高组织机构不能被删除!');
					return false;
				}
				var msg='确认删除<span style="color:red;font-weight:bold">'+groupName+'</span>组织机构?';
				createConfirmDialog('确认删除组织机构?',msg,function(){
					$.ajax({
						type : "GET",
						url : '<%=request.getContextPath()%>/group.do?method=deleteGroupById&id='+groupId,
						dataType : "json",
						success : function(data) {
							if (data['status'] == 'ERROR') {
								createAlertDialog('提示信息','删除组织机构失败!');
							} else {
								createAlertDialog('提示信息','删除组织机构成功!',function(){
									window.location.reload();
								});
							}
						},
						error : function() {
							createAlertDialog('提示信息','网络繁忙,请稍后再试!');
						}
					});
				},350,240);
			});
			
			$('#addUserToGroupBtn').die('click').live('click',function(){
				var path='<%=request.getContextPath()%>/group.do?method=addUserToGroup&id='+groupId+'&userids=';
				var params={
						  'title':'添加用户到组织机构',
						  'url':$(this).attr('url')+"?id="+groupId,
						  'frameSubmitType':'gridSelect',
						  'path':path,
						  'width':720,
						  'height':470,
						  btnSucFunc:function(){
							  $("#"+dataGridID).trigger("reloadGrid");
						  }
						 };
				createIframeDialog(params);
			});
			
			$('#delUserFromGroupBtn').die('click').live('click',function(){
				var userids; 
				userids = jQuery("#groupUserDataGrid").jqGrid('getGridParam','selarrrow');
				if(userids.length<=0){
					createAlertDialog('提示信息','请选择需要删除的用户!');
					return false;
				}
				var groupName=$("#"+groupId).find("a:not(ins)").text();
				var msg='确认从<span style="color:red;font-weight:bold">'+groupName+'</span>组织机构中删除id为('+userids+')的用户?';
				var alertMsg='从<span style="color:red;font-weight:bold">'+groupName+'</span>组织机构中删除id为('+userids+')的用户';
				createConfirmDialog('确认删除组织机构?',msg,function(){
					$.ajax({
						type : "GET",
						url : '<%=request.getContextPath()%>/group.do?method=deleteUsersFormGroupById&id='+groupId+'&userids='+userids,
						dataType : "json",
						success : function(data) {
							if (data['status'] == 'ERROR') {
								createAlertDialog('提示信息',alertMsg+'失败!');
							} else {
								createAlertDialog('提示信息',alertMsg+'成功!',function(){
									$("#"+dataGridID).trigger("reloadGrid");//刷新DataGrid数据
								});
							}
						},
						error : function() {
							createAlertDialog('提示信息','网络繁忙,请稍后再试!');
						}
					});
				});
			});
		});
		function getGroupInfoByTree(id){
			groupId=id;
			$("#groupInfoTr").html("");
			$(".topSpan").text("");
			$.ajax({//加载组织机构信息
				type : "GET",
				url : '<%=request.getContextPath()%>/group.do?method=getGroupById&id='+ id,
				dataType : "json",
				success : function(data) {
						if (data['status'] == 'ERROR') {
							alert(data['msg']);
						} else {
							var tdHtml = "<td>&nbsp;</td>";
							tdHtml += "<td title='"+data['departmentName']+"'>" + data['departmentName'] + "</td>";
							tdHtml += "<td title='"+data['telephone'] +"'>" + data['telephone'] + "</td>";
							if(data['parentGroup']['departmentName']==undefined){
								tdHtml += "<td id='noParentTr'>无</td>";
								$(".topSpan").text(data['departmentName']+"是系统的最高组织机构");
							}else{
								tdHtml += "<td><a class='upGroupLink' href='javascript:void(0)' id='"+data['parentGroup']['id']+"'>" + data['parentGroup']['departmentName'] + "</a></td>";
							}
							tdHtml += "<td title='"+data['description'] +"'>" + data['description'] + "</td>";
							$("#groupInfoTr").html(tdHtml);
							$(".upGroupLink").unbind('click').bind('click',function(){
								getGroupInfoByTree($(this).attr('id'));
							});
							//getUsersByGroupId(id);
						}
				},
				error : function() {
					createAlertDialog('提示信息', '网络繁忙,请稍后再试!');
				}
			});
		}
		function getUsersByGroupId(id){
			dataGridID='groupUserDataGrid';
			offsetWidth=210;
			var path='<%=request.getContextPath()%>/group.do?method=getEmployeesByGroupId&id='+ id;
			var params={
				'path':path,
				'colNames':[ 'ID', '员工编号', '真实姓名', '创建人', '创建时间' ],
				'colModel':[ {
					name : 'id',
					index : 'id',
					width : 10
					//百分比
					}, {
						name : 'userName',
						index : 'userName',
						width : 15
					}, {
						name : 'employeeName',
						index : 'employeeName',
						width : 15
					}, {
						name : 'createUserName',
						index : 'createUserName',
						width : 30
					}, {
						name : 'createDate',
						index : 'createDate',
						width : 30
				} ]
			};
			initJQGrid(params,dataGridID);
			$("#"+dataGridID).jqGrid('setGridParam', {
				url : path
			}).trigger("reloadGrid");//重新设置url重新加载数据

	}
</script>
<table width="100%" border='0' cellpadding='0' cellspacing='0'>
	<tr>
		<td style="padding: 4px 5px;"><a href='javascript:void(0);'
			id="newGroupBtn" url="./GroupAdd.jsp" class='zPushBtn' tabindex='-1'><img
				src="../../images/icons/icon042a2.gif" /><b>新建&nbsp;</b></a> <a
			href='javascript:void(0);' id='editGroupBtn' class='zPushBtn'
			url="<%=request.getContextPath()%>/group.do?method=updateGroup"
			tabindex='-1' id=''><img src="../../images/icons/icon042a4.gif" /><b>修改&nbsp;</b></a>
			<a href='javascript:void(0);' id='delGroupBtn' class='zPushBtn'
			tabindex='-1' id=''><img src="../../images/icons/icon042a3.gif" /><b>删除&nbsp;</b></a>
			<span class='topSpan' style='color:red;'></span></td>
	</tr>
	<tr>
		<td style="padding: 0px 5px;">
			<table width="100%" cellpadding="2" cellspacing="0" class="dataTable">
				<tr class="dataTableHead">
					<td width="5%">&nbsp;</td>
					<td width="15%"><b>名称</b></td>
					<td width="15%"><b>电话</b></td>
					<td width="20%"><b>上级机构</b></td>
					<td width="45%"><b>备注</b></td>
				</tr>
				<tr id="groupInfoTr">
				</tr>
			</table>
		</td>
	</tr>
	<tr>
		<td style="padding: 4px 5px;"><a href='javascript:void(0);'
			url='./GroupTabUserList.jsp' class='zPushBtn' tabindex='-1'
			id='addUserToGroupBtn'><img src="../../images/icons/icon021a2.gif" /><b>添加员工到组织机构&nbsp;</b></a>
			<a href='javascript:void(0);' class='zPushBtn' tabindex='-1' id='delUserFromGroupBtn'><img
				src="../../images/icons/icon021a3.gif" /><b>从组织机构中删除员工&nbsp;</b></a></td>
	</tr>
	<tr>
		<td id="dataGridTd"
			style="padding-top: 0px; padding-left: 6px; padding-right: 6px; padding-bottom: 8px;">
			<table id="groupUserDataGrid" width=100% class="dataGridTable"></table>
			<div id="pager"></div>
		</td>
	</tr>
</table>